/*
 * Copyright 2011 Angel Sanadinov
 *
 * This file is part of VBox WMI.
 *
 * VBox WMI is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * VBox WMI is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with VBox WMI.  If not, see <http://www.gnu.org/licenses/>.
 */

package Beans.Requests.UserManagement;

import Utilities.Functions;
import Utilities.PermissionsConstants;

/**
 * Instances of this class are used by the user interface when a user requests
 * changes to permissions to be made (create, remove, modify).
 *
 * @author Angel Sanadinov
 */
public class UserPermissionsRequest extends UserManagementRequest
{
    private String machinesPermissions; //the new machine permissions
    private String mediaPermissions;    //the new media permissions
    private String networksPermissions; //the new networks permissions

    /**
     * No-argument constructor. <br><br>
     * All fields are set to their invalid values.
     */
    public UserPermissionsRequest()
    {
        this.machinesPermissions = null;
        this.mediaPermissions = null;
        this.networksPermissions = null;
    }

    /**
     * Constructs the request object using the supplied data for permissions creation
     * or modification.
     *
     * @param requestorId the id of the user that requested the action
     * @param affectedUserId the id of the owner of the permissions
     * @param serverId the id of the server for which the permissions are valid
     * @param machinesPermissions the machines permissions
     * @param mediaPermissions the media permissions
     * @param networksPermissions the networks permissions
     * @param modify set to <code>true</code> if the specified permissions are to
     *               to be modified or <code>false</code> if the permissions are to be created
     */
    public UserPermissionsRequest(int requestorId, int affectedUserId, int serverId,
                                        String machinesPermissions, String mediaPermissions,
                                        String networksPermissions, boolean modify)
    {
        super.setRequestorId(requestorId);
        super.setServerId(serverId);
        super.setAffectedUserId(affectedUserId);
        super.setRequestedAction((modify) ? PermissionsConstants.ACTION_USER_MODIFY_PERMISSIONS
                                          : PermissionsConstants.ACTION_USER_CREATE_PERMISSIONS);

        this.machinesPermissions = machinesPermissions;
        this.mediaPermissions = mediaPermissions;
        this.networksPermissions = networksPermissions;
    }

    /**
     * Constructs the request object using the supplied data for permissions removal.
     *
     * @param requestorId the id of the user that requested the action
     * @param affectedUserId the id of the owner of the permissions
     * @param serverId the id of the server for which the permissions are valid
     */
    public UserPermissionsRequest(int requestorId, int affectedUserId, int serverId)
    {
        super.setRequestorId(requestorId);
        super.setServerId(serverId);
        super.setAffectedUserId(affectedUserId);
        super.setRequestedAction(PermissionsConstants.ACTION_USER_REMOVE_PERMISSIONS);
    }

    /**
     * Retrieves the machines permissions.
     *
     * @return the machines permissions
     */
    public String getMachinesPermissions()
    {
        return machinesPermissions;
    }

    /**
     * Retrieves the media permissions.
     *
     * @return the media permissions
     */
    public String getMediaPermissions()
    {
        return mediaPermissions;
    }

    /**
     * Retrieves the networks permissions.
     *
     * @return the networks permissions
     */
    public String getNetworksPermissions()
    {
        return networksPermissions;
    }

    /**
     * Sets the machines permissions.
     *
     * @param machinesPermissions the new machines permissions
     */
    public void setMachinesPermissions(String machinesPermissions)
    {
        this.machinesPermissions = machinesPermissions;
    }

    /**
     * Sets the media permissions.
     *
     * @param mediaPermissions the new media permissions
     */
    public void setMediaPermissions(String mediaPermissions)
    {
        this.mediaPermissions = mediaPermissions;
    }

    /**
     * Sets the networks permissions.
     *
     * @param networksPermissions the new networks permissions
     */
    public void setNetworksPermissions(String networksPermissions)
    {
        this.networksPermissions = networksPermissions;
    }

    /**
     * Checks the validity of the object. <br><br>
     *
     * This object is valid only when all data required for the specified request is valid.
     *
     * @return <code>true</code> if the object is valid or <code>false</code> otherwise
     */
    @Override
    public boolean isValid()
    {
        boolean result = false;

        //checks if the requestor id is valid
        if(!isRequestorIdValid())
            return result;
        else
            ;

        //based on the specified action, different checks are performed
        switch(getRequestedAction())
        {
            //modify and create actions need a owner id, server id, machine and media perm.
            case PermissionsConstants.ACTION_USER_CREATE_PERMISSIONS:
            case PermissionsConstants.ACTION_USER_MODIFY_PERMISSIONS:
            {
                if(isAffectedUserIdValid() && isServerIdValid()
                        && Functions.arePermissionsValid(machinesPermissions)
                        && Functions.arePermissionsValid(mediaPermissions)
                        && Functions.arePermissionsValid(networksPermissions))
                    result = true;
                else
                    result = false;
            } break;

            //remove action needs only an owner id and a server id
            case PermissionsConstants.ACTION_USER_REMOVE_PERMISSIONS:
            {
                if(isAffectedUserIdValid() && isServerIdValid())
                    result = true;
                else
                    result = false;
            } break;

            default: result = false;
        }

        return result;
    }

    /**
     * Returns a textual representation of the object. <br>
     *
     * It is in the form: "ClassName: (affectedUserId,machinesPermissions,
     * mediaPermissions,networksPermissions)".
     *
     * @return the object represented as a String
     */
    @Override
    public String toString()
    {
        return UserPermissionsRequest.class.getSimpleName()
                + ": (" + getAffectedUserId() + "," + machinesPermissions
                + "," + mediaPermissions + "," + networksPermissions + ")";
    }
}
